local filename = "bindwithdrawaccount.lua"

local timetool = require "timetool"
local skynet = require "skynet"
local cluster = require "skynet.cluster"
local filelog =require "filelog"
require "pubenum"

local service = nil
local CMD = {
	service = nil
}


--
--检测账户格式
--
local function check_account_format(account_type, accountname)
    local len = string.len(accountname)
    if len > 32 then
        return false
    end

    if account_type == EWithdrawType.EWITHDRAW_TYPE_ALIPAY         --提现到支付宝
    or account_type == EWithdrawType.EWITHDRAW_TYPE_WEIXIN then    --提现到微信
        local num0 = string.byte("0",1)
        local num9 = string.byte("9",1)
        local upperA = string.byte("A",1)
        local upperZ = string.byte("Z",1)
        local lowerA = string.byte("a",1)
        local lowerZ = string.byte("z",1)
        local dot = string.byte(".",1)
        local email = string.byte("@",1)
        local underline = string.byte("_",1)
        for i = 1,len do
            local val = string.byte(accountname,i)
            if (val >= num0 and val <= num9) 
            or (val >= lowerA and val <= lowerZ) 
            or (val == dot or val == email)
            or (val >= upperA and val <= upperZ)
            or (val == underline) then
                
            else
                return false
            end
        end
    elseif account_type == EWithdrawType.EWITHDRAW_TYPE_BANKCARD then  --提现到银行卡
        if len < 13 or len > 19 or string.sub(accountname,1,1) == '0' then   --银行卡13~19位
            return false
        end
        local min = string.byte("0",1)
        local max = string.byte("9",1)
        for i = 1,len do
            local val = string.byte(accountname,i)
            if val < min or val > max then
                return false
            end
        end
    else
        return false
    end

    return true
end

--
--绑定提现账号消息处理
--
function CMD.process(rid, msgbody)
	if not service then
		service = CMD.service
	end

    --验证参数是否合法
    local account_type = tonumber(msgbody.account_type) or 0
	local accountname = msgbody.accountname or ""
    local realname = msgbody.realname or ""
    local bankname = msgbody.bankname or ""
    if account_type == 0 or realname == "" or accountname == "" then
        return {retcode = EErrCode.ERR_INVALID_PARAMS,retstr = "无效的参数"}
    end

    --不能修改姓名
    if account_type == EWithdrawType.EWITHDRAW_TYPE_ALIPAY 
    and service.info.alipay_realname ~= "" and service.info.alipay_realname ~= realname then
        return {retcode = EErrCode.ERR_INVALID_PARAMS,retstr = "不能修改姓名"}
    elseif account_type == EWithdrawType.EWITHDRAW_TYPE_WEIXIN 
    and service.info.weixin_realname ~= "" and service.info.weixin_realname ~= realname then
        return {retcode = EErrCode.ERR_INVALID_PARAMS,retstr = "不能修改姓名"}
    elseif account_type == EWithdrawType.EWITHDRAW_TYPE_BANKCARD 
    and service.info.bankcard_realname ~= "" and service.info.bankcard_realname ~= realname then
        return {retcode = EErrCode.ERR_INVALID_PARAMS,retstr = "不能修改姓名"}
    end

    --真实姓名要与绑定手机时姓名相同
    --[[
    if service.info.realname ~= "" and service.info.realname ~= realname then
        return {retcode = EErrCode.ERR_INVALID_PARAMS,retstr = "无效的实名制姓名"}
    end
    ]]

    --检测姓名格式
    local len = string.len(realname)
    for i = 1,len do
        local val = string.byte(realname,i)
        if val <= 127 then
            return {retcode = EErrCode.ERR_INVALID_PARAMS,retstr = "姓名格式错误"}
        end
    end

    --检测账号格式
    local isok = check_account_format(account_type,accountname)
    if not isok then
        return {retcode = EErrCode.ERR_INVALID_PARAMS, retstr = "无效的账号格式"}
    end

    --更新数据
    local info = { }
    local otherinfo = { }
    local key = ""
    if account_type == EWithdrawType.EWITHDRAW_TYPE_ALIPAY then
         key = "alipay"
        info.alipayaccount = accountname
        otherinfo.alipay_realname = realname
    elseif account_type == EWithdrawType.EWITHDRAW_TYPE_WEIXIN then
         key = "weixin"
        info.weixinaccount = accountname
        otherinfo.weixin_realname = realname
    elseif account_type == EWithdrawType.EWITHDRAW_TYPE_BANKCARD then
        key = "bankcard"
        info.bankcardaccount = accountname
        info.bankcard_bankname = bankname
        otherinfo.bankcard_realname = realname

    end
	local result = cluster.call("datanode", ".router", "update_rk", rid, "rs_info", nil, info)
    if not result or result[1] ~= 1 then
        return {retcode = EErrCode.ERR_SYSTEM_ERROR, retstr = "系统错误"}
    end
    local key1 = key .. "account"
    local key2 = key .. "_realname"
    service.info[key1] = accountname
    service.info[key2] = realname
    if key == "bankcard" then
        service.info.bankcard_bankname = bankname
    end

    --保存实名信息
    cluster.call("datanode", ".router", "update_rk", rid, "rs_otherinfo", nil, otherinfo)

    --记录日志
    local logstr = string.format("%d,%d,%d,%s,%s", EGateAction.CHANGE_WITHDRAWACCOUNT, rid, account_type, accountname, realname)
    skynet.send(".gamedatalog", "lua", "write", logstr)

	return { }
end

return CMD